#!/bin/bash
#SBATCH --spread-job
# Copyright (c) 2018 Presto Labs Pte. Ltd.
set -ex

export OUTDIR="{{out_dir}}"
mkdir -p "${OUTDIR}"

WORK_DIR=$(pwd)

if [ -z ${SLURM_JOB_ID} ]; then
  (>&2 echo "ERROR: run this script on slurm.")
  exit 1
fi

source /remote/iosg/home/bot-hft/miniconda3/etc/profile.d/conda.sh
set +x
conda activate /remote/iosg/home/bot-hft/miniconda3/envs/coin2_motion_env
set -x

NUM_PARALLEL="$((${SLURM_NTASKS} + 30))"
parallel -j "${NUM_PARALLEL}" --delay {{job_submit_period}} <<EOF
{% for trading_date in trading_dates -%}
{% for parameter in parameter_map -%}
{% for machine in parameter["machines"] -%}
{% for exchange in parameter["exchanges"] -%}
{% for symbol in parameter["symbols"] -%}
{% for api_override in parameter["api_override"] -%}
{%- set api, recipe = api_override.split(".") -%}
{%- set mea = "{}.{}.{}".format(parameter["market_type"], exchange, api) -%}
{%- set filename = "{}/{}_{}_{}_{}_{}.png".format(out_dir, trading_date, machine, mea, recipe, symbol) -%}
srun -n1 -l --mem-per-cpu=4G --exclusive ../bazel-bin/cc/appcoin2/support/feed/plot --fastfeed --out="{{filename}}" --machine="{{machine}}" --mea="{{mea}}" --recipe="{{recipe}}" --product="{{symbol}}" --start="{{trading_date}}" --duration=PT24H
{% endfor %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
EOF

python -m coin.tool.mailer --name="coin-feed-plot" --mailfrom="coin-feed-plot@joomo.io" --mailto="{{summary_mailto}}" --subject="{{mail_subject}}" <<EOF
Slurm job ID:     ${SLURM_JOB_ID}
Output directory: ${OUTDIR}
User tag:         None

---
$(scontrol show job ${SLURM_JOB_ID})
EOF
